Imports BusinessLogic
Imports System.Net.Mail
Imports System.Xml

Partial Class Profile
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim _Profile As New BusinessLogic.Profile()
        _Profile.Username = Session("Username").ToString()
        _Profile.Address = txtAddress.Text.Trim()
        _Profile.City = txtCity.Text.Trim()
        _Profile.FirstName = txtFirstName.Text.Trim()
        _Profile.Gender = ddlGender.SelectedValue
        _Profile.LastName = txtLastName.Text.Trim()
        _Profile.State = txtState.Text.Trim()
        _Profile.Zip = txtZip.Text.Trim()
        _Profile.Email = txtEmail.Text.Trim()
        _Profile.BirthDay = Date.Parse(txtBirthDate.Text.Trim())
        _Profile.PhoneNumber = txtPhoneNumber.Text.Trim()

        _Profile.Save()

        lblMessage.Text = "Data saved successfully."

        Try

            ' create the email message 
            Dim _MailBody As String
            Dim oXmlDocument As New XmlDocument()
            oXmlDocument.Load(Server.MapPath("~/XML/ProfileMailBody.xml"))
            _MailBody = String.Format(oXmlDocument.InnerXml, _Profile.LastName, _Profile.FirstName, _Profile.Email, _Profile.Gender, _Profile.Address, _Profile.City, _Profile.State, _Profile.Zip, _Profile.BirthDay, _Profile.PhoneNumber)

            Dim message As New MailMessage()
            message.Body = _MailBody
            message.Subject = String.Format("Profile of {0} {1}", _Profile.LastName, _Profile.FirstName)
            message.To.Add(ConfigurationManager.AppSettings("ToMail"))
            ' create and add the attachment(s)
            'Dim attachment As New Attachment("sample.doc", MediaTypeNames.Application.Octet)
            'message.Attachments.Add(attachment)
            ' create SMTP Client and add credentials 
            Dim smtpClient As New SmtpClient()
            smtpClient.UseDefaultCredentials = True
            ' Email with Authentication 
            'smtpClient.Credentials = New Net.NetworkCredential("userID", "password", "domainName")
            'Send the message 
            smtpClient.Send(message)
        Catch ex As Exception

        End Try
        litJs.Text = "<script language=""javascript"" >Success();</script>"
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Session("Username") IsNot Nothing Then
                Dim _Profile As New BusinessLogic.Profile()
                _Profile.Username = Session("Username").ToString()
                _Profile.Load()

                txtAddress.Text = _Profile.Address
                txtCity.Text = _Profile.City
                txtFirstName.Text = _Profile.FirstName
                ddlGender.SelectedValue = _Profile.Gender
                txtLastName.Text = _Profile.LastName
                txtState.Text = _Profile.State
                txtZip.Text = _Profile.Zip
                txtEmail.Text = _Profile.Email
                txtPhoneNumber.Text = _Profile.PhoneNumber
                txtBirthDate.Text = _Profile.BirthDay.ToString("MM/dd/yyyy")
            Else
                Response.Redirect("~/login.aspx", True)

            End If
        End If
    End Sub

    Protected Sub btnfillOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnfillOut.Click
        pnlProfile.Visible = True
        plnMessage.Visible = False
    End Sub
End Class
